package com.whty.module.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whty.common.annotation.Log;
import com.whty.common.enums.BusinessType;
import com.whty.module.domain.Product;
import com.whty.module.service.IProductService;
import com.whty.web.core.base.BaseController;
import com.whty.framework.web.page.TableDataInfo;
import com.whty.common.base.AjaxResult;

/**
 * 产品 信息操作处理
 * 
 * @author mario
 * @date 2018-10-31
 */
@Controller
@RequestMapping("/module/product")
public class ProductController extends BaseController
{
    private String prefix = "module/product";
    private String prefix_detail = "module/productDetail";
    private static Logger logger = LoggerFactory.getLogger(ProductDetailController.class);
	@Autowired
	private IProductService productService;
	
	@RequiresPermissions("module:product:view")
	@GetMapping()
	public String product()
	{
	    return prefix + "/product";
	}
	
	/**
	 * 查询产品列表
	 */
	@RequiresPermissions("module:product:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Product product)
	{
		startPage();
        List<Product> list = productService.selectProductList(product);
		return getDataTable(list);
	}
	
	/**
	 * 新增产品
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存产品
	 */
	@RequiresPermissions("module:product:add")
	@Log(title = "产品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Product product)
	{		
		return toAjax(productService.insertProduct(product));
	}

	/**
	 * 修改产品
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		Product product = productService.selectProductById(id);
		mmap.put("product", product);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存产品
	 */
	@RequiresPermissions("module:product:edit")
	@Log(title = "产品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Product product)
	{		
		return toAjax(productService.updateProduct(product));
	}
	
	/**
	 * 删除产品
	 */
	@RequiresPermissions("module:product:remove")
	@Log(title = "产品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(productService.deleteProductByIds(ids));
	}
	
	
	 /**
     * 查询产品详细
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
//        mmap.put("product", dictTypeService.selectDictTypeById(dictId));
//        mmap.put("productList", dictTypeService.selectDictTypeAll());
      
        mmap.put("product",  productService.selectProductById(id));
        mmap.put("productList",productService.selectProductList(new Product()));
        return prefix_detail+"/productDetail";
    }
	/**
	 * 通过茶园编号查找溯源码
	 * @param teaGardenNo
	 * @return
	 */
    @PostMapping("/getProdunctCodeByGarden")
    @ResponseBody
    public List<String> getProdunctCodeByGarden(String teaGardenNo,String type){
    	logger.info("传入的厂商编号为："+teaGardenNo);
    	List<String> productCodeList = new ArrayList<String>();
    	if(StringUtils.isNotBlank(teaGardenNo)){
    		Product product = new Product();
    		product.setGardenNo(teaGardenNo);
    		if(StringUtils.isBlank(type)){
    			product.setStatus("00");
    		}
    		List<Product> productList = productService.selectProductList(product);
    		if(productList != null && productList.size() > 0){
    			for (Product product2 : productList) {
    				productCodeList.add(product2.getProductCode());
    			}
    		}
    		
    	}
    	return productCodeList;
    }
    
    /**
	 * 通过溯源码查找溯源码
	 * @param teaGardenNo
	 * @return
	 */
    @PostMapping("/getProdunctCodeByCode")
    @ResponseBody
    public List<String> getProdunctCodeByCode(String productCode,String type){
    	logger.info("传入的溯源码为："+productCode);
    	String gardenNo = "";
    	List<String> productCodeList = new ArrayList<String>();
    	if(StringUtils.isNotBlank(productCode)){
    		Product product = new Product();
    		product.setProductCode(productCode);
    		List<Product> productList = productService.selectProductList(product);
    		if(CollectionUtils.isNotEmpty(productList)){
    			gardenNo = productList.get(0).getGardenNo();
    		}
    		Product product2 = new Product();
    		if(gardenNo != null){
    			product2.setGardenNo(gardenNo);
    		}
    		if(StringUtils.isBlank(type)){
    			product2.setStatus("00");
    		}
    		List<Product> productList2 = productService.selectProductList(product2);
    		if(productList2 != null && productList2.size() > 0){
    			for (Product product3 : productList2) {
    				productCodeList.add(product3.getProductCode());
    			}
    		}
    		
    	}
    	return productCodeList;
    }
}
